﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;

public class RedNewMsgNotifiy
{
    static RedNewMsgNotifiy instance = new RedNewMsgNotifiy();

    public static RedNewMsgNotifiy I
    {
        get
        {
            if (instance == null)
                instance = new RedNewMsgNotifiy();

            return instance;
        }
    }

    //消息容器
    public List<RedRropMsg> m_MsgList = new List<RedRropMsg>();


    public RedNewMsgNotifiy() 
    {
        Init();
    }

    ~RedNewMsgNotifiy()
    {
        EventManager.Instance.unregister(EventManager.EventType.ET_NewRedMsg, NotifiyMsgEvent);
    }

    public void Init()
    {

        EventManager.Instance.register(EventManager.EventType.ET_NewRedMsg, NotifiyMsgEvent);
    }

    private void NotifiyMsgEvent(params object [] arg) 
    {
        SystemNofityType msgType = (SystemNofityType)arg[0];
        bool isAdd = (bool)arg[1];
        string id = (string)arg[2];


        if (isAdd)
        {
            Add(id, msgType);
        }
        else
        {
            Remove(id, msgType);
        }
    }

    private void Add(string id, SystemNofityType msgType)
    {
        m_MsgList.Add(new RedRropMsg() { Id = id, MsgType = msgType });
        EventManager.Instance.notify(EventManager.EventType.ET_ShowRedMsg, msgType, true);
    }

    private void Remove(string id, SystemNofityType msgType)
    {
        int isCount = 0;

        for (int i = 0; i < m_MsgList.Count; i++)
        {
            if (((msgType & m_MsgList[i].MsgType) > 0) && m_MsgList[i].Id.Equals(id))
            {
                m_MsgList.RemoveAt(i);
                isCount--;
                i--;
            }

            if (i < 0)
                break;

            if (((msgType & m_MsgList[i].MsgType) > 0))
            {
                isCount++;
            }
        }

        EventManager.Instance.notify(EventManager.EventType.ET_ShowRedMsg, msgType, (isCount > 0 && m_MsgList.Count > 0));
    }
    
    public bool IsMsg(SystemNofityType msgType) 
    {
        for (int i = 0; i < m_MsgList.Count; i++)
        {
            if (msgType == m_MsgList[i].MsgType) 
            {
                return true;
            }

            if ((m_MsgList[i].MsgType & msgType) > 0)
            {
                return true;
            }
        }
        return false;
    }

    public void RemoveAllMsg(SystemNofityType msgType)
    {
        for (int i = 0; i < m_MsgList.Count; i++)
        {
            if ((m_MsgList[i].MsgType & msgType) > 0)
                m_MsgList.RemoveAt(i);
        }

        foreach (int myCode in Enum.GetValues(typeof(SystemNofityType))) 
        {
            EventManager.Instance.notify(EventManager.EventType.ET_ShowRedMsg, msgType, false);
        }
    }
}


public class RedRropMsg
{
    public SystemNofityType MsgType { get; set; }
    public string Id { get; set; }
}

[Flags]
public enum SystemNofityType
{
    None = 1 << 0,
    Email = 1 << 1,
    Friend = 1 << 2,
    Bag = 1 << 3,
    Report = 1 << 4,
    Skill = 1 << 5,
    Welfare = 1 << 6,
    GameActivity = 1 << 7,

}

public interface INotifiySystem
{
    
}



